<template>
  <div v-if="visible" class="dict-modal-overlay" @click.self="handleClose">
    <div class="dict-modal-container">
      <div class="dict-modal-header">
        <h3>数据字典 - {{ dictType }}</h3>
        <button class="close-btn" @click="handleClose">&times;</button>
      </div>
      
      <div class="dict-modal-body">
        <div v-if="loading" class="loading">
          <div class="spinner"></div>
          <span>正在加载字典数据...</span>
        </div>
        
        <div v-else-if="error" class="error-message">
          <span class="error-icon">⚠️</span>
          {{ error }}
        </div>
        
        <div v-else-if="dictData.length > 0" class="dict-table-wrapper">
          <div class="dict-info">
            <span class="dict-count">共 {{ dictData.length }} 条记录</span>
            <span class="dict-type-label">{{ dictTypeLabel }}</span>
          </div>
          
          <table class="dict-table">
            <thead>
              <tr>
                <th class="col-dict-type-label">字典分类</th>
                <th class="col-dict-type">字典编码</th>
                <th class="col-code">代码</th>
                <th class="col-name">名称</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="item in dictData" :key="item.id">
                <td class="col-dict-type-label">{{ item.dictTypeLabel || '-' }}</td>
                <td class="col-dict-type">{{ item.dictType }}</td>
                <td class="col-code">{{ item.code }}</td>
                <td class="col-name">{{ item.name }}</td>
              </tr>
            </tbody>
          </table>
        </div>
        
        <div v-else class="no-data">
          <div class="no-data-icon">📝</div>
          <div class="no-data-text">暂无字典数据</div>
          <div class="no-data-desc">字典编码 {{ dictType }} 下没有找到相关数据</div>
        </div>
      </div>
      
      <div class="dict-modal-footer">
        <button class="btn btn-close" @click="handleClose">关闭</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DictDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    dictType: {
      type: String,
      required: true
    },
    dictData: {
      type: Array,
      default: () => []
    },
    loading: {
      type: Boolean,
      default: false
    },
    error: {
      type: String,
      default: null
    }
  },
  computed: {
    dictTypeLabel() {
      if (this.dictData.length > 0) {
        return this.dictData[0].dictTypeLabel || '未知分类'
      }
      return '未知分类'
    }
  },
  methods: {
    handleClose() {
      this.$emit('close')
    }
  },
  mounted() {
    // 添加ESC键关闭功能
    const handleEsc = (e) => {
      if (e.key === 'Escape' && this.visible) {
        this.handleClose()
      }
    }
    document.addEventListener('keydown', handleEsc)
    this.$once('hook:beforeDestroy', () => {
      document.removeEventListener('keydown', handleEsc)
    })
  }
}
</script>

<style scoped>
.dict-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2100;
  backdrop-filter: blur(2px);
}

.dict-modal-container {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 800px;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.dict-modal-header {
  padding: 20px 24px;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  color: white;
}

.dict-modal-header h3 {
  margin: 0;
  font-size: 1.3em;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  font-size: 28px;
  color: white;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: background 0.2s;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.dict-modal-body {
  padding: 20px 24px;
  overflow-y: auto;
  flex: 1;
  min-height: 200px;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: #6b7280;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #e5e7eb;
  border-top-color: #3b82f6;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.error-message {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px;
  background: #fee2e2;
  color: #dc2626;
  border-radius: 8px;
  border: 1px solid #fecaca;
}

.error-icon {
  font-size: 20px;
}

.dict-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding: 12px 16px;
  background: #f8fafc;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
}

.dict-count {
  font-weight: 600;
  color: #374151;
}

.dict-type-label {
  color: #6b7280;
  font-size: 14px;
}

.dict-table-wrapper {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
}

.dict-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.dict-table thead {
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
}

.dict-table th {
  padding: 12px 16px;
  text-align: left;
  font-weight: 600;
  color: #374151;
  border-bottom: 2px solid #d1d5db;
}

.dict-table td {
  padding: 12px 16px;
  border-bottom: 1px solid #e5e7eb;
  vertical-align: top;
}

.dict-table tbody tr:hover {
  background: #f9fafb;
}

.dict-table tbody tr:nth-child(even) {
  background: #f8fafc;
}

.dict-table tbody tr:nth-child(even):hover {
  background: #f1f5f9;
}

/* 列宽控制 */
.col-dict-type-label {
  width: 25%;
}

.col-dict-type {
  width: 15%;
  font-family: 'Courier New', monospace;
  font-weight: 600;
  color: #3b82f6;
}

.col-code {
  width: 20%;
  font-family: 'Courier New', monospace;
  font-weight: 500;
  color: #059669;
}

.col-name {
  width: 40%;
}

.no-data {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #9ca3af;
}

.no-data-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.no-data-text {
  font-size: 18px;
  font-weight: 500;
  color: #6b7280;
  margin-bottom: 8px;
}

.no-data-desc {
  font-size: 14px;
  color: #9ca3af;
}

.dict-modal-footer {
  padding: 16px 24px;
  border-top: 1px solid #e5e7eb;
  display: flex;
  justify-content: flex-end;
  background: #f9fafb;
}

.btn {
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  border: none;
}

.btn-close {
  background: #6b7280;
  color: white;
}

.btn-close:hover {
  background: #4b5563;
  transform: translateY(-1px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .dict-modal-container {
    width: 95%;
    max-height: 95vh;
  }
  
  .dict-modal-header {
    padding: 16px 20px;
  }
  
  .dict-modal-header h3 {
    font-size: 1.1em;
  }
  
  .dict-modal-body {
    padding: 16px 20px;
  }
  
  .dict-info {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .dict-table {
    font-size: 12px;
  }
  
  .dict-table th,
  .dict-table td {
    padding: 8px 12px;
  }
  
  /* 移动端表格滚动 */
  .dict-table-wrapper {
    overflow-x: auto;
  }
  
  .dict-table {
    min-width: 600px;
  }
}
</style>